package com.yanqu.road.server.manger.activity.auto.send;

import com.yanqu.road.entity.activity.ActivityConditionInfo;
import com.yanqu.road.entity.activity.ActivityInfo;
import com.yanqu.road.entity.activity.UnionActivityConditionData;
import com.yanqu.road.entity.activity.UserActivityConditionData;
import com.yanqu.road.entity.enums.eUnionPosition;
import com.yanqu.road.entity.union.UnionInfo;
import com.yanqu.road.logic.bussiness.player.UserActivityBussiness;
import com.yanqu.road.pb.activity.AutoSendRewardProto;
import com.yanqu.road.pb.crossunion.activity.CrossUnionActivityRankDataProto;
import com.yanqu.road.server.manger.union.CrossUnionMgr;
import com.yanqu.road.server.manger.union.UnionActivityMgr;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class FiveHeroAutoSend extends BaseAutoSendActivity {

    public FiveHeroAutoSend(ActivityInfo activityInfo) {
        super(activityInfo);
    }

    @Override
    public void sendLocalUserRankReward() {

    }

    @Override
    public void sendLocalUnionRankReward() {

    }

    @Override
    protected boolean updateConditionData(UserActivityConditionData data) {
        return UserActivityBussiness.updateUserActivityConditionData(data);
    }


    @Override
    public void sendConditionReward() {

    }

    @Override
    public List<UserActivityConditionData> getNeedSendUserActivityConditionData() {
        return null;
    }

    @Override
    public Map<String, UnionActivityConditionData> getUnionActivityConditionData(int conditionType) {
        return UserActivityBussiness.getUnionActivityConditionDataMap(activityInfo.getActivityId(), conditionType);
    }

    @Override
    public Map<Long, UserActivityConditionData> getUserActivityRankConditionData(int activityId, int conditionType) {
        return getUserNormalActivityRankConditionData(activityId, conditionType);
    }

    @Override
    public void sendCrossUnionRankReward(List<AutoSendRewardProto.UnionRankMsg> dataList) {
        if (dataList.size() <= 0) {
            AutoSendActivity.getLogger().info("activity unionRank data size is 0, activityId:{}, activityName:{}", activityInfo.getActivityId(), activityInfo.getActivityName());
        } else {
            Map<Integer, List<AutoSendRewardProto.UnionRankMsg>> userRankMsgMap = new HashMap<>();
            for (AutoSendRewardProto.UnionRankMsg msg : dataList) {
                int conditionType = msg.getConditionType();
                String unionId = msg.getUnionId();
                UnionInfo unionInfo = CrossUnionMgr.getUnionInfo(unionId);
                if (unionInfo != null) {
                    if (!userRankMsgMap.containsKey(conditionType)) {
                        userRankMsgMap.put(conditionType, new ArrayList<>());
                    }
                    List<AutoSendRewardProto.UnionRankMsg> list = userRankMsgMap.get(conditionType);
                    list.add(msg);
                }
            }
            for (Map.Entry<Integer, List<AutoSendRewardProto.UnionRankMsg>> entry : userRankMsgMap.entrySet()) {
                Integer conditionType = entry.getKey();
                Map<Long, UserActivityConditionData> conditionDataMap = getUserActivityRankConditionData(activityInfo.getActivityId(), conditionType);
                List<AutoSendRewardProto.UnionRankMsg> rankMsgList = entry.getValue();
                for (AutoSendRewardProto.UnionRankMsg msg : rankMsgList) {
                    for (UserActivityConditionData data : conditionDataMap.values()) {
                        long userId = data.getUserId();
                        CrossUnionActivityRankDataProto.CrossUnionMemberPosDataMsg posDataMsg = null;
                        for (CrossUnionActivityRankDataProto.CrossUnionMemberPosDataMsg tmpMsg : msg.getPosListList()) {
                            if (tmpMsg.getUserId() == userId) {
                                posDataMsg = tmpMsg;
                                break;
                            }
                        }
                        if (posDataMsg == null) {
                            continue;
                        }
                        if (data.isGetReward()) {
                            continue;
                        }
                        data.setGetReward(true);
                        int unionRank = msg.getRank();
                        int campRank = msg.getCampRank();
                        ActivityConditionInfo conditionInfo = getCampUnionRankCondition(conditionType, campRank, unionRank); // 【这个榜多了个阵营条件】
                        if (conditionInfo != null && updateConditionData(data)) {
                            String rewardStr;
                            if (posDataMsg.getPos() == eUnionPosition.Master.getValue()) {
                                rewardStr = conditionInfo.getMasterRewardStr();
                            } else {
                                //根据职位获取奖励
                                rewardStr = UnionActivityMgr.getUnionMemberPositionDataReward(posDataMsg.getPos(), conditionInfo, userId);
                            }
                            CrossUnionMgr.getRewardHonourItem(rewardStr, activityInfo,unionRank,msg.getUnionId(),userId);
                            rewardStr = CrossUnionMgr.removeHonourItem(rewardStr);
                            AutoSendActivity.getLogger().info("autoSend unionReward, user: {}, activity: {},conditionType: {}, campRank: {}, unionRank: {}, reward: {}", userId, activityInfo.getActivityId(), conditionType, campRank, unionRank, rewardStr);
                            sendMailReward(userId, rewardStr);
                        }
                    }
                }
            }
        }
        AutoSendActivity.changeSendState(activityInfo.getActivityId(), 2, 1);
    }

    private ActivityConditionInfo getCampUnionRankCondition(int conditionType, int campRank, int unionRank) {
        List<ActivityConditionInfo> infoList = conditionInfoMap.get(conditionType);
        if (null != infoList) {
            for (ActivityConditionInfo conditionInfo : infoList) {
                // 阵营排名要对上
                if (conditionInfo.getParamList().get(2).intValue() != campRank) {
                    continue;
                }
                // 再拿商会排名
                if (unionRank >= conditionInfo.getParamList().get(0).intValue() && unionRank <= conditionInfo.getParamList().get(1).intValue()) {
                    return conditionInfo;
                }
            }
        }
        return null;
    }
    @Override
    public void noticeCrossUserRank() {
        noticeCrossUserGroupRank();
    }

    public void noticeCrossUnionRank() {
        noticeCrossUnionGroupRank();
    }
}
